package com.xge.component_caipu

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.xge.common_libary.bean.CaiPuListInfoSection
import com.xge.common_libary.bean.CollectCpEntity
import com.xge.component_caipu.binder.ItemCollectContentBinder
import com.xge.component_caipu.room.AppDatabase
import com.xge.component_caipu.room.dao.CollectDao
import kotlinx.coroutines.launch
import me.drakeet.multitype.MultiTypeAdapter
import om.xge.component_caipu.databinding.FragmentCollectBinding

class CollectFragment : Fragment() {

    private lateinit var binding: FragmentCollectBinding
    private var isCheckHide = true
    private lateinit var adapter: MultiTypeAdapter
    private var cheks = arrayListOf<String>() // 用于存储 sid
    private var list = arrayListOf<CollectCpEntity>()

    private lateinit var collectDao: CollectDao

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = FragmentCollectBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }

    private fun refreshData() {
        lifecycleScope.launch {
            list.clear()
            list.addAll(collectDao.getAll())
            adapter.notifyDataSetChanged()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val db = AppDatabase.getInstance(requireContext())
        collectDao = db.collectDao()

        adapter = MultiTypeAdapter(list)
        adapter.register(
            CollectCpEntity::class.java, object :ItemCollectContentBinder({ isCheckHide }, cheks){
                override fun onItemClick(item: CollectCpEntity) {
                    val b = Gson().fromJson(item.content,CaiPuListInfoSection::class.java)
                    CaiPuDetailActivity.start(requireContext(),b)
                }
            })
        binding.rvContent.adapter = adapter

        binding.tvEdit.setOnClickListener {
            if (binding.tvDel.isGone) {
                binding.tvDel.isGone = false
                binding.tvDel.text = "删除"
                isCheckHide = false
                binding.tvEdit.text = "完成"
            } else {
                binding.tvDel.isGone = true
                isCheckHide = true
                cheks.clear()
                binding.tvEdit.text = "编辑"
            }
            adapter.notifyDataSetChanged()
        }

        binding.tvDel.setOnClickListener {
            lifecycleScope.launch {
                try {
                    collectDao.deleteByIds(cheks)
                    cheks.clear()
                    binding.tvDel.isGone = true
                    binding.tvEdit.text = "编辑"
                    refreshData()
                    ToastUtils.showShort("删除成功")
                    isCheckHide = true
                } catch (e: Exception) {
                    e.printStackTrace()
                    ToastUtils.showShort("删除失败")
                }
            }
        }
    }


    override fun onPause() {
        super.onPause()
        binding.tvEdit.text = "编辑"
        binding.tvDel.isGone = true
        isCheckHide = true
        cheks.clear()
    }

    companion object {
        fun newInstance() = CollectFragment()
    }
}
